<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class StorageModel extends Model
{
    use HasFactory;

    protected $table = 'storage';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    static public function getStorageList($parentId, $data)
    {
        return SELF::select('a.*', 'b.nickname', 'b.username')
                    ->from('storage as a')
                    ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
                    ->where($parentId)
                    ->where('a.delete_flag', 0)
                    ->orderBy('a.id', 'DESC')
                    ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
    }

    static public function getStorageSelect($parentId)
    {
        return SELF::select(
                        'id',
                        'storage_name',
                        'storage_address'
                    )
                    ->where($parentId)
                    ->where('delete_flag', 0)
                    ->orderBy('id', 'DESC')
                    ->get();
    }
}
